import { BrandingEnum } from '@/common';
import { useUserInfo } from '@/store/userStore';
import { useEffect, useState } from 'react';


const usePermission = () => {
  const [isAdmin, setIsAdmin] = useState(true);
  const [isSuperAdmin, setIsSuperAdmin] = useState(true);
  const [isPermit, setIspermit] = useState(true);
  const [isGuest, setIsGuest] = useState(true);
  const [isInManager, setIsInManager] = useState(false);
  const { role } = useUserInfo();

  useEffect(() => {
    if (!role) {
      return;
    }
    role?.code && setIspermit(role?.code != 'student' && role?.code != 'anonymous');
    role?.code && setIsAdmin(role?.code === 'admin' || role?.code?.includes('admin'));
    role?.code && setIsSuperAdmin(role?.code === 'super_admin' || role?.code?.includes('super_admin'));
    role?.code && setIsGuest(role?.code === 'guest' || role?.code?.includes('guest'));
  }, [role]);

  useEffect(() => {
    const url = new URL(window.location.href);
    const pathname = url.pathname;
    if (pathname.includes(BrandingEnum?.permission_manage) || pathname.includes(BrandingEnum?.app_orchestration)) {
      setIsInManager(true);
    }
  }, []);

  return { isAdmin, isSuperAdmin, isPermit, isGuest, isInManager };
};

export default usePermission;
